<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/10/11
 * Time: 11:52
 */

namespace app\admin\service;

use lib\common\CommonService;
use model\AdminPermissionModel;
use model\AdminPermissionUserModel;
use model\AdminUserModel;
use think\facade\Db;
use think\Paginator;

class UserService extends CommonService
{
    private AdminUserModel $adminUserModel;
    private AdminPermissionUserModel $adminPermissionUserModel;

    private AdminPermissionModel $adminPermissionModel;

    /**
     * @param AdminUserModel $adminUserModel
     * @param AdminPermissionUserModel $adminPermissionUserModel
     * @param AdminPermissionModel $adminPermissionModel
     */
    public function __construct (AdminUserModel $adminUserModel, AdminPermissionUserModel $adminPermissionUserModel, AdminPermissionModel $adminPermissionModel) {
        parent::__construct();
        $this->adminUserModel = $adminUserModel;
        $this->adminPermissionUserModel = $adminPermissionUserModel;
        $this->adminPermissionModel = $adminPermissionModel;
    }

    public function create ($params): void {
        $this->transaction(function () use ($params) {
            $params['password'] = password_hash($params['password'], PASSWORD_BCRYPT);
            $user = $this->adminUserModel::create($params, $this->adminUserModel::ALLOWED_FIELDS);
            $this->adminPermissionUserModel->saveBatch($user['id'], $params['permissions']);
        });
    }

    public function update ($id, $params): void {
        if (!empty($params['password'])) {
            $params['password'] = password_hash($params['password'], PASSWORD_BCRYPT);
        } else {
            unset($params['password']);
        }
        $this->transaction(function () use ($params, $id) {
            $this->adminPermissionUserModel->where('user_id', $id)->delete();
            $user = $this->adminUserModel->findUser($id);
            $user->allowField($this->adminUserModel::ALLOWED_FIELDS)->save($params);
            $this->adminPermissionUserModel->saveBatch($user['id'], $params['permissions']);
        });
    }

    public function delete ($id): void {
        $this->transaction(function () use ($id) {
            $this->adminUserModel->findUser($id)->delete();
            $this->adminPermissionUserModel->where('user_id', $id)->delete();
        });
    }

    public function getList ($params): Paginator {
        $where = [];
        if (!empty($params['username'])) {
            $where[] = ['username|mobile|full_name', 'like', '%' . trim($params['username']) . '%'];
        }
        if (isset($params['status']) && intval($params['status']) > 0) {
            $where[] = ['status', '=', intval($params['status'])];
        }
        return $this->adminUserModel
            ->where($where)
            ->order(['id' => 'ASC'])
            ->hidden(['password'])
            ->paginate($this->getPageSize());
    }

    public function findUser ($id): AdminUserModel {
        $data = $this->adminUserModel->findUser($id);
        $permission = $this->adminPermissionModel->where('id', 'in', Db::raw($this->adminPermissionUserModel->where('user_id', $id)->field('permission_id')->buildSql()))->select()->toArray();
        $data['permissions'] = $permission;
        return $data;
    }
}
